<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>07-构造函数</title>
    <script src="lib/react/react.development.js"></script>
    <script src="lib/react/react-dom.development.js"></script>
    <script src="lib/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      class HelloWorld extends React.Component {
        constructor() {
          super();
          console.log("1. 构造函数");
          this.nowTime = new Date().getTime();
        }
        componentDidMount() {
          console.log("3. 渲染完成");
          setTimeout(() => {
            this.nowTime = "新时间";
            // 从时间戳上可以看到,大概3秒之后打印出新时间,但页面并未渲染
            console.log(new Date().getTime(), this.nowTime);
          }, 3000);
        }
        componentWillUnMount() {
          console.log("组件即将销毁...");
        }
        render() {
          console.log("2. render...");
          return (
            <div className={"danger large"}>
              <h2>倒计时</h2>
              {/* className和class属性是一样的,而且必须是驼峰 */}
              <span>{this.nowTime}</span>
            </div>
          );
        }
      }
      // 通过调用React自身方法render可以得到当前组件的实例对象,并渲染到页面容器.
      ReactDOM.render(<HelloWorld />, document.getElementById("root"));
    </script>
  </body>
</html>
